package com.luoye.lj.util.codegenerator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import static com.baomidou.mybatisplus.annotation.DbType.MYSQL;
import static com.baomidou.mybatisplus.generator.config.rules.NamingStrategy.underline_to_camel;
/**
 * Created with IntelliJ IDEA.
 *
 * @author dave_suwei
 * @date 2019/6/22 20:52
 */
@Deprecated
public class CodeGeneratorUtil {

    private static void generate(CodeGeneratorConfig cgc) {
        //基本配置
        //数据源 dataSourceConfig 配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbQuery(new MySqlQuery());
        dsc.setDbType(MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert());
        dsc.setUrl(cgc.getUrl());
        dsc.setDriverName(cgc.getDriverName());
        dsc.setUsername(cgc.getUsername());
        dsc.setPassword(cgc.getPassword());
        //数据库表配置
        StrategyConfig sc = new StrategyConfig();
        //数据库表映射到实体的命名策略
        sc.setNaming(underline_to_camel);
        //数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
        sc.setColumnNaming(underline_to_camel);
        //表前缀
        sc.setTablePrefix(cgc.getTablePreFix());
        //Boolean类型字段是否移除is前缀（默认 false）
        sc.setEntityBooleanColumnRemoveIsPrefix(true);
        //生成 @RestController 控制器
        sc.setRestControllerStyle(false);
        //驼峰转连字符
        sc.setControllerMappingHyphenStyle(true);
        //需要包含的表名，允许正则表达式（与exclude二选一配置）
        sc.setInclude(cgc.getInclude());
        //包名配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(cgc.getParent());
        pc.setEntity("pojo.dbo");
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        gc.setOpen(false);
        gc.setAuthor(cgc.getAuthor());
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        gc.setEntityName("%sDbo");
        gc.setMapperName("%sDao");
        gc.setControllerName("%sController");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setXmlName("%sMapper");
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        mpg.setDataSource(dsc);
        mpg.setPackageInfo(pc);
        mpg.setStrategy(sc);
        mpg.setGlobalConfig(gc);
        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {
        CodeGeneratorConfig config = new CodeGeneratorConfig();
        config.setUrl("jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf-8&useSSL=false");
        config.setUsername("root");
        config.setPassword("lzg123");
        //包含的表：只生成了最基础需要的表:信息支付订单表 订单评价表 个人信息订单表 任务信息订单表 后期需要什么表 自己再选择生成
        config.setInclude(new String[]{"t_test"});
        config.setTablePreFix("t_");
        //config.setProjectPath("E:\\Developer Space\\sales_champion_order_service");
        config.setAuthor("luo");
        CodeGeneratorUtil.generate(config);
    }
}
